// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {colorType} from "./HeroControl";
import HeroContact, {colliderGroup} from "./HeroContact";
import {emitType} from "./GameData_red_blue_man";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import Music, {musicName} from "../PublicScript/Music";
import ControlScript from "../PublicScript/ControlScript";
import CollisionManager = cc.CollisionManager;

const {ccclass, property} = cc._decorator;

@ccclass
export default class DorControl extends cc.Component {

    @property({tooltip: "门的类型", type: cc.Enum(colorType)})
    dorType: colorType = colorType.red;

    @property({tooltip: "门框和门图片", type: cc.SpriteFrame})
    spriteFrameList: cc.SpriteFrame[] = [];

    @property({tooltip: "门精灵组件", type: cc.Sprite})
    doorSprite: cc.Sprite[] = [];

    //移动状态
    moveState = false;
    //是否已经可以开门
    openState = false;
    //获取门的高度
    doorHeight = 0;
    //
    pos = cc.v2(0,0);

    onLoad () {
        cc.game.on(emitType.getKey, this.setSky, this);
        cc.game.on(emitType.createOverPage,()=>{
            this.anim(-1, true);
        }, this);
        this.init();
    }

    start () {

    }

    init(){
        if(this.dorType == colorType.blue){
            this.doorSprite[0].spriteFrame = this.spriteFrameList[0];
            this.doorSprite[1].spriteFrame = this.spriteFrameList[2]
        }else{
            this.doorSprite[0].spriteFrame = this.spriteFrameList[1];
            this.doorSprite[1].spriteFrame = this.spriteFrameList[3]
        }
        this.doorHeight = this.doorSprite[1].node.height;
        this.node.getPosition(this.pos);
    }



    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact(contact, self, other) {
        if(GameControl_red_blue_man.gameState){
            return;
        }
        if(other.node.group == colliderGroup.hero/*"hero"*/ && other.tag == "0"){
            let otherState = other.node.getComponent(HeroContact).manType;
            if(otherState == this.dorType && this.openState){
                if(this.dorType == colorType.red){
                    cc.game.emit("newPlay");
                }
                other.node.getComponent(HeroContact).changeContactDoor(true, this.pos.x);
                GameControl_red_blue_man.doorState[this.dorType] = true;
                if(GameControl_red_blue_man.doorState[colorType.red] == true && GameControl_red_blue_man.doorState[colorType.blue] == true){
                    other.body.linearVelocity = cc.v2(0,0);
                    GameControl_red_blue_man.gameState = true;
                    cc.game.emit(emitType.gameOver);
                }
                this.moveState = true;
                this.anim(1);
            }
        }
    }

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact(contact, self, other) {
        if(GameControl_red_blue_man.gameState){
            return;
        }
        if(other.node.group == colliderGroup.hero/*"hero"*/ && other.tag == "0"){
            let otherState = other.node.getComponent(HeroContact).manType;
            other.node.getComponent(HeroContact).changeContactDoor(false);
            if(otherState == this.dorType && this.openState){
                GameControl_red_blue_man.doorState[this.dorType] = false;
                this.moveState = false;
                this.anim(-1);
            }
        }
    }

    actionSpeed = 250;
    canPlayeffect = true;
    //动画
    anim(dir, over?){
        if(this.canPlayeffect){
            Music.GamePlayEffect(musicName.stone_door);
        }
        this.canPlayeffect = false;

        this.doorSprite[1].node.stopAllActions();
        let endY = dir == 1 ? this.doorHeight : 0;
        //获取现在所在的位置       路程 = 时间 * 速度
        let now = this.doorSprite[1].node.y;
        let time = 0;
        let v = 0;
        if(dir == 1){
            v = this.doorHeight - now;
        }else{
            v = now
        }
        time = v / this.actionSpeed;
        //计算时间
        this.doorSprite[1].node.runAction(
            cc.sequence(
                cc.moveTo(time, cc.v2(this.doorSprite[1].node.x, endY)),
                cc.callFunc(()=>{
                    this.canPlayeffect = true;
                    if(over){
                        cc.game.emit(emitType.loadOverPage);
                    }
                })
            )

        )
    }

    //获取现在钥匙所在的状态
    setSky(node,type){
        if(type == this.dorType){
            this.openState = true;
        }
    }

    // update (dt) {}
}
